package com.su.gulimall.cart.controller;

import com.su.gulimall.cart.interceptor.CartInterceptor;
import com.su.gulimall.cart.service.CartService;
import com.su.gulimall.cart.vo.Cart;
import com.su.gulimall.cart.vo.CartItem;
import com.su.gulimall.cart.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.concurrent.ExecutionException;

@Controller
public class IndexController {

    @Autowired
    CartService cartService;

    @RequestMapping({"/", "index.html", "success.html", "index", "success"})
    public String indexPage() {
        return "success";
    }

    @GetMapping("/cart.html")
    public String cartListPage(Model model) throws ExecutionException, InterruptedException {
         Cart cart=cartService.getCart();
         model.addAttribute("cart",cart);
         return "cartList";
    }
     //添加商品到购物车
    @GetMapping("/addToCart")
    //添加哪个商品，添加几个
    public String addToCart(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num, RedirectAttributes ra) throws ExecutionException, InterruptedException {
         cartService.addToCart(skuId,num);
         ra.addAttribute("skuId",skuId);
         return "redirect:http://cart.gulimall.com/addToSuccess.html";
    }
    @GetMapping("/addToSuccess.html")
    public String addToSuccessPage(@RequestParam("skuId") Long skuId,Model model)
    {
       CartItem cartItem=cartService.getCartItem(skuId);
       model.addAttribute("cartItem",cartItem);
       return "success";
    }
    //购物项状态动态改变
    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId,@RequestParam("check") Integer check)
    {
        cartService.checkItem(skuId,check);
        return "redirect:http://cart.gulimall.com/cart.html";
    }
    @GetMapping("/itemNum")
    public String itemNum(@RequestParam("skuId") Long skuId,@RequestParam("num") Integer num){
        cartService.itemNum(skuId,num);
        return "redirect:http://cart.gulimall.com/cart.html";
    }
    @GetMapping("/delItem")
    public String delItem(@RequestParam("skuId") Long skuId)
    {
        cartService.delItem(skuId);
        return "redirect:http://cart.gulimall.com/cart.html";
    }
    @GetMapping("/getCheckedCartItem")
    @ResponseBody
    public List<CartItem> getCheckedCartItem()
    {
       return cartService.getCheckedCartItem();
    }
}

